package com.ship.dispatch.feign;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ship.dispatch.bean.*;
import com.ship.dispatch.bean.treaShipNetwork.IhsShipBaseList;
import com.ship.dispatch.service.*;
import com.ship.dispatch.service.report.ReportItemService;
import com.ship.system.api.domain.dispatch.ShipInfoUpdateDto;
import com.ship.system.api.domain.dispatch.SimpleShipInfoVo;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zhangyh
 */
@Api(tags = "调度服务Feign远程调用客户端")
@RestController
@RequestMapping("/dispatchFeign")
public class DispatchClient {

    @Autowired
    private SpShipScheduleService spShipScheduleService;

    @Autowired
    private SmShipCrewService smShipCrewService;

    @Autowired
    private SpYearPlanSchemeDetailService spYearPlanSchemeDetailService;

    @Autowired
    private SpDispatchSchemeDetailService spDispatchSchemeDetailService;

    @Autowired
    private SpArrangeSchemeDetailService spArrangeSchemeDetailService;

    @Autowired
    private SpDispatchSchemeExecuteService spDispatchSchemeExecuteService;

    @Autowired
    private ReportItemService reportItemService;

    @Autowired
    private HomePageService homePageService;

    @Autowired
    private SpInformationService spInformationService;

    @Autowired
    private SpPortRouteDistanceService spPortRouteDistanceService;

    @Autowired
    private ConShippingService conShippingService;

    @GetMapping(value = "/portRouteRelationUpdate")
    public void portRouteRelationUpdate(){
        spPortRouteDistanceService.portRouteRelationUpdate();
    }

    @GetMapping(value = "/dispatchExpireMessageSend")
    public void dispatchExpireMessageSend(){
        //调度提醒任务
        spArrangeSchemeDetailService.dispatchExpireMessageSend();
    }

    /**
     * 获取船舶信息（船舶管理）
     */
    @GetMapping(value = "/getShioCounty")
    public String getShioCounty(@RequestParam(value = "shipName") String shipName, @RequestParam(value = "imo")  String imo){
        IhsShipBaseList baseList = spInformationService.queryShipBaseByShipName(shipName,imo);
        if(baseList != null){
            return baseList.getFlagName();
        }
        return null;
    }

    /**
     * 数据看板缓存更新
     */
    @GetMapping(value = "/reportItemCacheUpdate")
    public void reportItemCacheUpdate(){
        reportItemService.reportItemCacheUpdate();
    }

    /**
     * 获取全球船舶信息
     */
    @GetMapping(value = "/getAllSimpleShipList")
    public List<SimpleShipInfoVo> getAllSimpleShipList(){
        return spInformationService.getAllSimpleShipList();
    }

    /**
     * 首页大屏数据缓存更新
     */
    @GetMapping(value = "/homeStatisticsCacheUpdate")
    public void homeStatisticsCacheUpdate(){
        homePageService.getStatistics(0);
    }

    /**
     * 船员上下船状态校验
     */
    @GetMapping(value = "/checkShipCrewStatus")
    public void checkShipCrewStatus(){
        smShipCrewService.checkShipCrewStatus();
    }

    /**
     * 船舶信息更新
     * @param dto
     * @return
     */
    @PostMapping("/updateDispatchInfo")
    public void updateDispatchInfo(@RequestBody ShipInfoUpdateDto dto){
        //更新年度计划
        LambdaUpdateWrapper<SpYearPlanSchemeDetail> yearSchemeWra = new LambdaUpdateWrapper<SpYearPlanSchemeDetail>();
        yearSchemeWra.eq(SpYearPlanSchemeDetail::getResourceContract,dto.getId());
        yearSchemeWra.set(StringUtils.isNotBlank(dto.getBuyer()),SpYearPlanSchemeDetail::getBuyer,dto.getBuyer());
        yearSchemeWra.set(StringUtils.isNotBlank(dto.getSeller()),SpYearPlanSchemeDetail::getSeller,dto.getSeller());
        yearSchemeWra.set(StringUtils.isNotBlank(dto.getContractName()),SpYearPlanSchemeDetail::getContractName,dto.getContractName());
        yearSchemeWra.set(dto.getTradeBody() != null,SpYearPlanSchemeDetail::getTradeBody,dto.getTradeBody());
        yearSchemeWra.set(dto.getTradeType() != null,SpYearPlanSchemeDetail::getTradeType,dto.getTradeType());
        spYearPlanSchemeDetailService.update(yearSchemeWra);
        //更新调度优化
        LambdaUpdateWrapper<SpDispatchSchemeDetail> disSchemeWra = new LambdaUpdateWrapper<SpDispatchSchemeDetail>();
        disSchemeWra.eq(SpDispatchSchemeDetail::getResourceContract,dto.getId());
        disSchemeWra.set(StringUtils.isNotBlank(dto.getBuyer()),SpDispatchSchemeDetail::getBuyer,dto.getBuyer());
        disSchemeWra.set(StringUtils.isNotBlank(dto.getSeller()),SpDispatchSchemeDetail::getSeller,dto.getSeller());
        disSchemeWra.set(StringUtils.isNotBlank(dto.getContractName()),SpDispatchSchemeDetail::getContractName,dto.getContractName());
        disSchemeWra.set(dto.getTradeBody() != null,SpDispatchSchemeDetail::getTradeBody,dto.getTradeBody());
        disSchemeWra.set(dto.getTradeType() != null,SpDispatchSchemeDetail::getTradeType,dto.getTradeType());
        spDispatchSchemeDetailService.update(disSchemeWra);
    }

    /**
     * 船舶信息更新
     * @param dto
     * @return
     */
    @PostMapping("/updateShipInfo")
    public Boolean updateShipInfo(@RequestBody ShipInfoUpdateDto dto){
        //更新船期表
        LambdaUpdateWrapper<SpShipSchedule> scheduleWra = new LambdaUpdateWrapper<SpShipSchedule>();
        scheduleWra.eq(SpShipSchedule::getImo,dto.getImo());
        scheduleWra.set(StringUtils.isNotBlank(dto.getShipName()),SpShipSchedule::getShipName,dto.getShipName());
        scheduleWra.set(StringUtils.isNotBlank(dto.getShipImo()),SpShipSchedule::getImo,dto.getShipImo());
        scheduleWra.set(dto.getCabinCapacity() != null,SpShipSchedule::getStorageCapacity,dto.getCabinCapacity());
        spShipScheduleService.update(scheduleWra);
        //更新年度计划
        LambdaUpdateWrapper<SpYearPlanSchemeDetail> yearSchemeWra = new LambdaUpdateWrapper<SpYearPlanSchemeDetail>();
        yearSchemeWra.eq(SpYearPlanSchemeDetail::getImo,dto.getImo());
        yearSchemeWra.set(StringUtils.isNotBlank(dto.getShipName()),SpYearPlanSchemeDetail::getShipName,dto.getShipName());
        yearSchemeWra.set(StringUtils.isNotBlank(dto.getShipImo()),SpYearPlanSchemeDetail::getImo,dto.getShipImo());
        yearSchemeWra.set(StringUtils.isNotBlank(dto.getContractName()),SpYearPlanSchemeDetail::getContractName,dto.getContractName());
        yearSchemeWra.set(dto.getCabinCapacity() != null,SpYearPlanSchemeDetail::getLoadingCapacity,dto.getCabinCapacity());
        yearSchemeWra.set(dto.getTradeBody() != null,SpYearPlanSchemeDetail::getTradeBody,dto.getTradeBody());
        spYearPlanSchemeDetailService.update(yearSchemeWra);
        //更新调度优化
        LambdaUpdateWrapper<SpDispatchSchemeDetail> disSchemeWra = new LambdaUpdateWrapper<SpDispatchSchemeDetail>();
        disSchemeWra.eq(SpDispatchSchemeDetail::getImo,dto.getImo());
        disSchemeWra.set(StringUtils.isNotBlank(dto.getShipName()),SpDispatchSchemeDetail::getShipName,dto.getShipName());
        disSchemeWra.set(StringUtils.isNotBlank(dto.getShipImo()),SpDispatchSchemeDetail::getImo,dto.getShipImo());
        disSchemeWra.set(StringUtils.isNotBlank(dto.getContractName()),SpDispatchSchemeDetail::getContractName,dto.getContractName());
        disSchemeWra.set(dto.getCabinCapacity() != null,SpDispatchSchemeDetail::getLoadingCapacity,dto.getCabinCapacity());
        disSchemeWra.set(dto.getTradeBody() != null,SpDispatchSchemeDetail::getTradeBody,dto.getTradeBody());
        spDispatchSchemeDetailService.update(disSchemeWra);
        //更新船货排布
        LambdaUpdateWrapper<SpArrangeSchemeDetail> arrSchemeWra = new LambdaUpdateWrapper<SpArrangeSchemeDetail>();
        arrSchemeWra.eq(SpArrangeSchemeDetail::getImo,dto.getImo());
        arrSchemeWra.set(StringUtils.isNotBlank(dto.getShipName()),SpArrangeSchemeDetail::getShipName,dto.getShipName());
        arrSchemeWra.set(StringUtils.isNotBlank(dto.getShipImo()),SpArrangeSchemeDetail::getImo,dto.getShipImo());
        arrSchemeWra.set(StringUtils.isNotBlank(dto.getContractName()),SpArrangeSchemeDetail::getContractName,dto.getContractName());
        arrSchemeWra.set(dto.getCabinCapacity() != null,SpArrangeSchemeDetail::getLoadingCapacity,dto.getCabinCapacity());
        arrSchemeWra.set(dto.getTradeBody() != null,SpArrangeSchemeDetail::getTradeBody,dto.getTradeBody());
        spArrangeSchemeDetailService.update(arrSchemeWra);
        //更新船运执行
        LambdaUpdateWrapper<SpDispatchSchemeExecute> excSchemeWra = new LambdaUpdateWrapper<SpDispatchSchemeExecute>();
        excSchemeWra.eq(SpDispatchSchemeExecute::getImo,dto.getImo());
        excSchemeWra.set(StringUtils.isNotBlank(dto.getShipName()),SpDispatchSchemeExecute::getShipName,dto.getShipName());
        excSchemeWra.set(StringUtils.isNotBlank(dto.getShipImo()),SpDispatchSchemeExecute::getImo,dto.getShipImo());
        excSchemeWra.set(StringUtils.isNotBlank(dto.getContractName()),SpDispatchSchemeExecute::getContractName,dto.getContractName());
        excSchemeWra.set(dto.getCabinCapacity() != null,SpDispatchSchemeExecute::getLoadingCapacity,dto.getCabinCapacity());
        spDispatchSchemeExecuteService.update(excSchemeWra);
        return true;
    }

    /**
     * 获取可用船只imo
     */
    @GetMapping(value = "/setShipImo")
    public void setShipImo(){
        conShippingService.setShipImo();
    }

    /**
     * 获取可用船只的Ais状态
     */
    @GetMapping(value = "/setImoStatus")
    public void setImoStatus(){
        conShippingService.setImoStatus();
    }
}
